<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />

		<script src="/js/plugins/jrender/jrender.min.js"></script>

		<script src="/js/plugins/form/jquery.form.js"></script>
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">

		<script>
            $(function () {
                //取出用户信息
                var json = JSON.parse(sessionStorage.getItem('user'));
                //使用jrender插件 设置渲染数据
                $('.info').renderValues(json);

                //设置背景图片img标签的数据回显 通过样式background-image
				$('.myBg').css('background-image','url('+ json.coverImgUrl +')');

				//保存提交操作  因为每个员工id不同,请求的资源也不同,要拼接资源
				$('#saveBtn').click(function () {
				    $('#infoForm').attr('action','/users/'+json.id);
					$('#infoForm').ajaxSubmit(function (data) {
						if(data.success){
						    //更新sessionStorage的用户信息
							sessionStorage.setItem('user',JSON.stringify(data.user));
						    window.parent.location.href="/mine/profiles.html";
						}else{
                            $(document).dialog({
                                content:data.msg
                            })
						}
                    })
                });

				//声明变量用于区分头像与背景图
				var flag=true;

				//头像按钮出文件上传框事件
				$('#headBtn').click(function () {
                    flag=true;
					//触发文件上传框
					$('#btn-file').click();
                });

                //背景图按钮出文件上传框事件
                $('#coverBtn').click(function () {
                    flag=false;
                    //触发文件上传框
                    $('#btn-file').click();
                });

				//当文件上传框的值改变就发请求更新
				$('#fileForm').change(function () {
					//判断是否有上传文件
					if($('#btn-file').val()){
					    //提交表单
						$('#fileForm').ajaxSubmit(function (data) {
							//根据返回结果判断上传结果
							if(data.status==1){
							    //判断是头像还是背景图
								if(flag){ //true 为头像
                                    //更新回显数据
                                    $('#headImg').attr('src',data.url);
                                    $('input[name="headImgUrl"]').val(data.url);
                                }else{
                                    $('.myBg').css('background-image','url('+ data.url +')');
                                    $('input[name="coverImgUrl"]').val(data.url);
								}
							}else{
                                $(document).dialog({
                                    content:data.msg
                                })
							}
                        })
					}
                })


            })
		</script>

	</head>
	<body>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<form id="fileForm" method="post" action="/images" enctype="multipart/form-data">
			<input type="file" id="btn-file" style="display: none" name="file">
		</form>

		<div class="container info">
			<div class="myBg">
				<img id="headImg"  class="rounded-circle" render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" >
					<input type="hidden" name="id" render-value="id">
					<!-- 更改提交方式 -->
					<input type="hidden" name="_method" value="put">
					<input type="hidden" name="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">

					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
					<label>性别</label><select class="form-control" name="gender" render-value="gender">
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
